﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using Billing.Utilities;
using Billing.Controls;

namespace Billing.Windows
{
    public partial class CancelBulkOrder : Window
    {
        bool isValid = false;

        public CancelBulkOrder()
        {
            InitializeComponent();
            billNo.Focus();
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter && billNo.Text != string.Empty)
                {
                    DataSet dsDetails = Database.GetBulkOrderDetails(billNo.Text);

                    if (dsDetails != null)
                    {
                        DateTime deliveryDt = DateTime.Parse(dsDetails.Tables[0].Rows[0]["DeliveryDate"].ToString());
                        deliveryDate.Content = deliveryDt.DayOfWeek.ToString() + "-" + deliveryDt.ToString("dd-MMM-yyyy") + " " + dsDetails.Tables[0].Rows[0]["DeliveryTime"].ToString();
                        deliveryPlace.Content = dsDetails.Tables[0].Rows[0]["DeliveryPlace"].ToString();
                        customerName.Content = dsDetails.Tables[0].Rows[0]["CustomerName"].ToString();
                        phoneNo.Content = dsDetails.Tables[0].Rows[0]["PhoneNo"].ToString();
                        totalAmount.Content = dsDetails.Tables[0].Rows[0]["TotalAmount"].ToString();
                        additionalDetails.Content = dsDetails.Tables[0].Rows[0]["Description"].ToString();
                        bookedDate.Content = DateTime.Parse(dsDetails.Tables[0].Rows[0]["BookedDate"].ToString()).ToString("dd-MMM-yyyy");

                        lbItems.Items.Clear();

                        if (dsDetails.Tables[0].Rows[0]["EachBoxCost"].ToString() == "0")
                            eachBoxCost.Content = "-";
                        else
                            eachBoxCost.Content = dsDetails.Tables[0].Rows[0]["EachBoxCost"].ToString();

                        if (dsDetails.Tables[0].Rows[0]["Qty"].ToString() == "0")
                            qty.Content = "-";
                        else
                            qty.Content = dsDetails.Tables[0].Rows[0]["Qty"].ToString();

                        foreach (DataRow row in dsDetails.Tables[1].Rows)
                        {
                            BulkOrderControl ctrl = new BulkOrderControl();
                            ctrl.lblCode.Content = row["ItemCode"].ToString();
                            ctrl.lblItem.Content = row["ItemName"].ToString();
                            ctrl.lblQty.Content = row["Qty"].ToString();
                            lbItems.Items.Add(ctrl);
                        }
                        lblMsg.Text = "Enter Reason & Press F1 to Cancel Bulk Order";
                        isValid = true;

                        TimeSpan ts = deliveryDt - DateTime.Now;

                        if (bool.Parse(dsDetails.Tables[0].Rows[0]["IsOrderDelivered"].ToString()))
                        {
                            lblMsg.Text = "Order Already Delivered.";
                            isValid = false;
                        }
                        else if (bool.Parse(dsDetails.Tables[0].Rows[0]["IsOrderCancelled"].ToString()))
                        {
                            lblMsg.Text = "Order Already Cancelled.";
                            isValid = false;
                        }
                        else if (!(deliveryDt > DateTime.Now && deliveryDt.Day != DateTime.Now.Day))
                        {
                            lblMsg.Text = "Order Cannot Be Cancelled. Too Late.";
                            isValid = false;
                        }
                    }
                    else
                    {
                        lblMsg.Text = "Invalid Bulk Order Bill #. Please Enter Valid Bill No.";
                        isValid = false;
                        customerName.Content = string.Empty;
                        phoneNo.Content = string.Empty;
                        totalAmount.Content = string.Empty;
                        additionalDetails.Content = string.Empty;
                        bookedDate.Content = string.Empty;
                        lbItems.Items.Clear();
                        deliveryDate.Content = string.Empty;
                    }
                    if (isValid)
                    {
                        cancelReason.Focus();
                    }
                }
                else if (e.Key == Key.F1 && isValid)
                {
                    int result = Database.CancelBulkOrder(billNo.Text, cancelReason.Text);
                    if (result < 0)
                    {
                        MessageBox.Show("Some Unknown Error Has Occurred.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Bulk Order Cancelled Successfully.", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
                        this.Close();
                    }
                }
                else if (e.Key == Key.F1 && !isValid)
                {
                    MessageBox.Show("Not a Valid Order to Cancel.", "Invalid", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                else if (e.Key == Key.Escape)
                {
                    this.Close();
                }
            }
            catch (Exception) { }
        }
    }
}
